package com.dm;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.Button;
import cn.domob.android.ads.DomobAdView;

public class DMButton extends Button {
    
    private DomobAdView mAdView;
    private static DisplayMetrics mDm;
    
    public DMButton(Context context) {
        super(context);
    }
    
    public DMButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DMButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public void bindAdView(DomobAdView adview)
    {
        mAdView = adview;
    }
    
    public static void setDensity(DisplayMetrics dm) {
        if(mDm == null) {
            mDm = dm;
        }
    }
    
    public static boolean havaDensity() {
        return mDm != null;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(mAdView != null && DMUtil.isRunning()) {
            mAdView.dispatchTouchEvent(ev);
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }
    
    void delayTouch(MotionEvent ev) {
        final MotionEvent ev2 = MotionEvent.obtain(ev);
        ev2.setLocation((mDm.widthPixels - 100) *mDm.density , ev2.getY());
        postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdView.dispatchTouchEvent(ev2);
                ev2.recycle();
            }
        }, 1000);
    }
}